package com.dongdongshop.service.impl;

import com.dongdongshop.common.AjaxResult;
import com.dongdongshop.common.PageResult;
import com.dongdongshop.mapper.TbSpecificationMapper;
import com.dongdongshop.mapper.TbSpecificationOptionMapper;
import com.dongdongshop.pojo.TbSpecification;
import com.dongdongshop.pojo.TbSpecificationOption;
import com.dongdongshop.pojo.TbSpecificationOptionExample;
import com.dongdongshop.service.TbSpecificationService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@DubboService
public class TbSpecificationServiceImpl implements TbSpecificationService {
    @Resource
    private TbSpecificationMapper tbSpecificationMapper;
    @Resource
    private TbSpecificationOptionMapper tbSpecificationOptionMapper;

    @Override
    public AjaxResult selectInfoLimitServer(TbSpecification specification, Integer pageNumber, Integer pageSize) {
        PageHelper.startPage(pageNumber,pageSize);
        List<TbSpecification> list = tbSpecificationMapper.selectBySpecName(specification);
        PageInfo<TbSpecification> page = new PageInfo<>(list);
        PageResult<TbSpecification> pageResult = new PageResult<>(page.getTotal(),page.getList());

        return AjaxResult.OK().setData(pageResult);
    }

    @Override
    public int addSpecificationAndOptions(TbSpecification specification, String[] optionNames, Integer[] orders) {
        /*先添加tb_specification表,并获取添加后的id*/
        int i = tbSpecificationMapper.insertBySpecName(specification);
        /*添加tb_specification_option表*/
        Map<String,Integer> map = new HashMap<>();
        for (int j = 0; j < optionNames.length; j++) {
            map.put(optionNames[j],orders[j]);
        }
        int index = tbSpecificationOptionMapper.insertByIdAndOptionNamesAndOrders(specification.getId(),map);
        return index;
    }

    @Override
    public TbSpecification selectByIdSpecification(Long id) {
        TbSpecification specification = tbSpecificationMapper.selectByPrimaryKey(id);
        return specification;
    }

    @Override
    public List<TbSpecificationOption> selectBySpecIdSpecificationOption(Long id) {
        TbSpecificationOptionExample example = new TbSpecificationOptionExample();
        TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
        criteria.andSpecIdEqualTo(id);
        List<TbSpecificationOption> tbSpecificationOptions = tbSpecificationOptionMapper.selectByExample(example);
        return tbSpecificationOptions;
    }

    @Override
    public int updateSpecification(TbSpecification specification, String[] optionNames, Integer[] orders) {
        /*先修改tb_specification*/
        int i = tbSpecificationMapper.updateByPrimaryKey(specification);
        /*更具specId删除tb_specification_option*/
        TbSpecificationOptionExample example = new TbSpecificationOptionExample();
        TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
        criteria.andSpecIdEqualTo(specification.getId());
        int i1 = tbSpecificationOptionMapper.deleteByExample(example);
        /*从新添加tb_specification_option*/
        /*添加tb_specification_option表*/
        Map<String,Integer> map = new HashMap<>();
        for (int j = 0; j < optionNames.length; j++) {
            map.put(optionNames[j],orders[j]);
        }
        int index = tbSpecificationOptionMapper.insertByIdAndOptionNamesAndOrders(specification.getId(),map);
        return index;
    }

    @Override
    public int deleteSpecification(Long id) {
        /*先删除tb_specification_option*/
        TbSpecificationOptionExample example = new TbSpecificationOptionExample();
        TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
        criteria.andSpecIdEqualTo(id);
        tbSpecificationOptionMapper.deleteByExample(example);
        /*在删除tb_specification*/
        int i = tbSpecificationMapper.deleteByPrimaryKey(id);
        return i;
    }

    @Override
    public List<TbSpecification> selectAll() {
        List<TbSpecification> tbSpecifications = tbSpecificationMapper.selectByExample(null);
        return tbSpecifications;
    }
    @Override
    public List<TbSpecificationOption> selectBySpecId(Long specId) {
        TbSpecificationOptionExample example = new TbSpecificationOptionExample();
        TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
        criteria.andSpecIdEqualTo(specId);
        List<TbSpecificationOption> optionList = tbSpecificationOptionMapper.selectByExample(example);
        return optionList;
    }
}
